/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Chatree Siangklom
 * Created On : Jan 15, 2013  5:50:54 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.common
 * File Name : SyncAttachFileAction.java
 * TODO
 */
package apps.common;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.promis.common.Company;
import jpa.entitys.promis.common.CostProject;
import jpa.entitys.promis.common.CostProjectPK;
import jpa.entitys.synchro.staging.AGenDesc;
import jpa.entitys.synchro.staging.SyncAttachFile;
import jpa.service.promis.common.CompanyServices;
import jpa.service.promis.common.ProjectServices;
import jpa.service.promis.common.SyncAttachFilesServices;
import jpa.service.synchro.staging.SyncAttachFileServices;
import apps.ApplConfiguration;
import apps.sessionFactory;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 13, 2013 : 2:26:36 PM 
 * CreateBy=Chatree Siangklom
 * 
 * SyncAttachFileAction
 * TODO
 */
public class SyncAttachFileAction {
	
	private static final Logger log = Logger.getLogger(SyncAttachFileAction.class);	
	private String instanceNo = null;
	
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:26:44 PM 
	 * CreateBy=Chatree Siangklom
	 * String
	 * TODO
	 * @return
	 */
	public String getInstanceNo() {
		return instanceNo;
	}

	/**
	 * 
	 * Date =Nov 13, 2013 : 2:26:49 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param instanceNo
	 */
	public void setInstanceNo(String instanceNo) {
		this.instanceNo = instanceNo;
	}

	/**
	 * 
	 * @param module
	 * @param company
	 * @param project
	 * @param type
	 * @param series
	 * @param number
	 * @return
	 * @throws Exception
	 * Exm : "PR", "SP", "CRRH", "PR", "1208", "6"
	 */
	public String [] GetAttachFile(String module, String company, String project, String type, String series, String number, String revision) throws Exception {
		try{
			
			EntityManager emPromis = sessionFactory.CreateEmPromis();
			SyncAttachFilesServices object = new SyncAttachFilesServices(emPromis);
			
			String result = object.GetAttachFile(module, company, project, type, series, number, revision);
			if(!CommonService.IsNull(result)){
				System.out.println(result);
				String [] str1 = result.split(",");
				
				System.out.println("length =" + str1.length);
				/*
				for(int i=0;i < str1.length; i++)
		        {
		            System.out.println("Split1 Element "+i+" :"+str1[i]);
		        }
		        */
				
				if(str1 != null){
					if(str1.length > 0){
						return str1;
					}
				}					
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			throw e;
		}
		return null;
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:26:54 PM 
	 * CreateBy=Chatree Siangklom
	 * List<SyncAttachFile>
	 * TODO
	 * @param module
	 * @param company
	 * @param project
	 * @param type
	 * @param series
	 * @param number
	 * @param revision
	 * @param attach_file
	 * @return
	 * @throws Exception
	 */
	public List<SyncAttachFile> CreateToStaging(String module, String company, String project, String type, String series, String number, String revision, String [] attach_file) throws Exception {
		try{
			
			if(CommonService.IsNull(module)) throw new Exception("Error Require field module !!!");
			else if(CommonService.IsNull(company)) throw new Exception("Error Require field company !!!");
			else if(CommonService.IsNull(project)) throw new Exception("Error Require field project !!!");
			else if(CommonService.IsNull(type)) throw new Exception("Error Require field type !!!");
			else if(CommonService.IsNull(series)) throw new Exception("Error Require field series !!!");
			else if(CommonService.IsNull(number)) throw new Exception("Error Require field number !!!");
						
			if(attach_file != null){								
				if(attach_file.length > 0){
					List<SyncAttachFile> vlauelist = new ArrayList<SyncAttachFile>();
					EntityManager emSynchro = sessionFactory.CreateEmSynchro();
					EntityManager emPromis = sessionFactory.CreateEmPromis();
					jpa.service.synchro.staging.SyncAttachFileServices objj = new jpa.service.synchro.staging.SyncAttachFileServices(emSynchro);
					
					if(CommonService.IsNull(getInstanceNo()))
						setInstanceNo(objj.GetNewInstanceNo());			
					
					if(CommonService.IsNull(getInstanceNo())) {
						emSynchro = null;
						emPromis = null;
						objj = null;
						vlauelist = null;
						throw new Exception("Error Require field instanceNo !!!");
					}
					for(int i = 0; i < attach_file.length; i++)
					{
						SyncAttachFile model = new SyncAttachFile();
						model.setInstanceNo(getInstanceNo());
						model.setModule(module);
						model.setCompany(company);
						model.setProject(project);
						model.setDocumentNo(CommonService.ConvertPromisDocToDocumentNo(type, series, number, revision));
						
						AGenDesc aGenDesc = new ApplConfiguration().GetPromisUrlHost();
						if(aGenDesc != null){
							System.out.println(CommonService.IsNullRString(aGenDesc.getValue1()) + CommonService.IsNullRNull(attach_file[i]));
							model.setFilePath(CommonService.IsNullRString(aGenDesc.getValue1()) + CommonService.IsNullRNull(attach_file[i]));
						}else
							throw new Exception("Error Find not found PROMIS URL HOST !!!");
						
						model.setFileSequenceNo(CommonService.ConvertStringToBigDecimal((i+1)+""));
						
						//Company
						Company companySrv = new CompanyServices(emPromis).findById(company);
						if(companySrv != null)
							model.setSapCompanyCode(companySrv.getSapCompanyCode());
						else
							throw new Exception("Data not found Company =" + company);
						
						//Project
						CostProjectPK projectId = new CostProjectPK();
						projectId.setCompany(company);
						projectId.setProject(project);
						CostProject costProject = new ProjectServices(emPromis).findById(projectId);						
						if(costProject != null)
							model.setSapPlantCode(costProject.getSapPlantCode());
						else
							throw new Exception("Data not found project =" + project);
						
						model.setStatus("A");
						vlauelist.add(model);	
					}
					
					//Insert to Promis				
					objj.Inserted(vlauelist);
					
					//find ByInstance 
					vlauelist = new ArrayList<SyncAttachFile>();
					vlauelist = objj.findByInstanceNo(getInstanceNo());
					if(CommonService.getSize(vlauelist) > 0)
						return vlauelist;
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			throw e;
		}
		return null;
	}
	
	/**
	 * 
	 * @param instanceNo
	 * @return
	 * @throws Exception
	 */
	public List<SyncAttachFile> findByInstanceNo (String instanceNo) throws Exception {
		EntityManager emSynchro = null;
		SyncAttachFileServices SyncAttachFileServ = null;
		List<SyncAttachFile> vlauelist = null;
		try{
			
			emSynchro = sessionFactory.CreateEmSynchro();
			SyncAttachFileServ = new SyncAttachFileServices(emSynchro);
			vlauelist = SyncAttachFileServ.findByInstanceNo(instanceNo);			
			
			return (CommonService.getSize(vlauelist) > 0) ? vlauelist : null;
			
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			System.out.println(e.getMessage());
			throw e;
		}finally{
			emSynchro = null;
			SyncAttachFileServ = null;
		}
	}
	
	/**
	 * Date =Nov 13, 2013 : 2:27:01 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param lst
	 * @throws Exception
	 */
	public void Updated(List<SyncAttachFile> lst) throws Exception {
		EntityManager emSynchro = null;
		SyncAttachFileServices SyncAttachFileServ = null;
		try{
			if(CommonService.getSize(lst) > 0){
				emSynchro = sessionFactory.CreateEmSynchro();
				SyncAttachFileServ = new SyncAttachFileServices(emSynchro);
				SyncAttachFileServ.Updated(lst);
				
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			throw e;
		}finally{
			emSynchro = null;
			SyncAttachFileServ = null;
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			SyncAttachFileAction object = new SyncAttachFileAction();
			/*
			String [] attach_file =  object.GetAttachFile("PR", "SP", "SSL", "PR", "1211", "1", null);
			List<SyncAttachFile> vlauelist = object.CreateToStaging("PR", "SP", "SSL", "PR", "1211", "1", null, attach_file);
			System.out.println(CommonService.getSize(vlauelist));
			*/
			
			List<SyncAttachFile> vlauelist = object.findByInstanceNo("10000000000001081652");
			if(CommonService.getSize(vlauelist) > 0){
				for(SyncAttachFile value : vlauelist){
					System.out.println(value.getFileSequenceNo() + "," + value.getFilePath());
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
